package cn.com.w.jdk.thread.pool.timer;

import cn.com.w.jdk.thread.pool.vo.ListTestData;
import cn.com.w.jdk.thread.pool.worker.SendDataWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;

@Configuration     //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduledTask {

    Logger log = LoggerFactory.getLogger(ScheduledTask.class);


    @Resource(name = "jdkExector")
    Executor executor;

    //3.添加定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    public void configureTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }
    
    @Scheduled(fixedRate=5000)
    public void sendDataTasks() {
        log.info("开始执行定时任务，时间：[{}]", new Date());
        sendData();
    }

    public void sendData() {

        List<String> stringList = ListTestData.getTestData();

        Iterator<String> iterator = stringList.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            // jdk线程池
            executor.execute(new SendDataWorker(str));
            int maximumPoolSize =  Runtime.getRuntime().availableProcessors();
            log.info("服务器可使用进程数量：[{}]", maximumPoolSize);

        }
    }

}